#coding: utf-8
'''
one part of IMUmultiFunctionalPen(Motion Pen) --added at 2018-7-14 
角度单位：弧度
last edited at 2018-7-15

2018-7-15 by Stone
1.Delete the unnecessary code and modified some funcitons.
2.Add screen resolution auto-adpation feature.
3.Add the pause button feature.
4.Continuous click feature for specific game use.
'''
import win32api, win32con #, win32gui
import visual
import math

# =========================================================
# =======    dates
# =========================================================
# for screen
# times 0.8 to reduce vabration with cost of smaller region
SCR_SIZE = (0.8 * win32api.GetSystemMetrics(0),
            win32api.GetSystemMetrics(1))

# for uer opration and making event
M_LEFTDOWN  = win32con.MOUSEEVENTF_LEFTDOWN
M_LEFTUP    = win32con.MOUSEEVENTF_LEFTUP
M_RIGHTDOWN = win32con.MOUSEEVENTF_RIGHTDOWN
M_RIGHTUP   = win32con.MOUSEEVENTF_RIGHTUP
M_MOVE      = win32con.MOUSEEVENTF_MOVE

BT_DOWN = 0 #按键按下为0
ifHeld = {'lf':False, 'ri':False, 3:False}

ifPause = False

        
# =========================================================
# =======    Functons
# =========================================================
def buttonEvent(button):
    '上拉电阻，0即按下'
    global ifPause
    if button[3] == BT_DOWN and ifHeld[3] == False:
        print '3 down!!!', ifHeld[3] #debug
        win32api.mouse_event(M_LEFTDOWN,0,0,0,0)
        ifHeld[3] = True
    elif (not (button[3] == BT_DOWN)) and ifHeld[3] == True:
        print '3 up!!!', ifHeld[3] #debug
        win32api.mouse_event(M_LEFTUP,0,0,0,0)
        ifHeld[3] = False

    if button['lf'] == BT_DOWN:
        # not detect held so as to continuous click
        win32api.mouse_event(M_LEFTDOWN,0,0,0,0)
        print 'lf down!!!' #debug
        win32api.mouse_event(M_LEFTUP,0,0,0,0)
        print 'lf up!!!'  #debug

    if button['ri'] == BT_DOWN and ifHeld['ri'] == False:
        print 'ri down!!!'
        ifHeld['ri'] = True
    elif (not (button['ri'] == BT_DOWN)) and ifHeld['ri'] == True:
        print 'pause change'
        ifHeld['ri'] = False
        ifPause = not ifPause
    

def low_pass_filter(old, new):
    k = 0.3
    result = k * old + (1 - k) * new

    return result

    
def normalize(v):
    return visual.vector(v).norm()
    

def MapCursorPos(rx,ry):
    '''摇杆模式的光标坐标映射函数
       rx,ry 为重力在传感器XoZ平面上的投影,为方便理解光标坐标的映射，
       使用rx，ry作变量名。rx,ry总为正。
    '''
    pos = [0,0]
    pos[0] = int(rx * SCR_SIZE[0])
    pos[1] = int(ry * SCR_SIZE[1])
    
    win32api.SetCursorPos(pos)
    print pos
    
    return 'cursor moved to(%i,%i)'%(pos[0],pos[1])

    
def MouseMain(RwEst, button):
    '本模块主函数,用来完成本模块的所有操作,方便上级程序的调用'
    global rx, ry
    RwEst = normalize(RwEst)
    rx = low_pass_filter(rx, RwEst[2])
    ry = low_pass_filter(ry, -RwEst[1])
    
    rx = (rx + 1.0)/(1+1)    # +1则在传感器坐标下向右平移一个单位，使其总为正值 
    ry = (ry + 1.0)/(1+1)  # +0.8,避免手后仰太多。
    
    if ifPause:
        print 'pausing'
    else:
        MapCursorPos(rx, ry)
    
    buttonEvent(button)

# ==============================
# initialization
# ==============================
rx = 0
ry = 0
# =========================================================
# =======    Test
# =========================================================
if __name__ == '__main__': 
    for i in range(300):
        # 抛物线鼠标轨迹
        ymax = 150**2
        Rx = (float(i*2) + 100) / SCR_SIZE[0]
        Ry = -float((i-150)**2) /ymax + 1
        
        MapCursorPos(Rx,Ry)
        visual.rate(100)
    
